package com.geilizu.dom.controller;

import com.geilizu.dom.dto.BuildingDto;
import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.entity.DormitoryAdmin;
import com.geilizu.dom.entity.Student;
import com.geilizu.dom.result.ResponseCode;
import com.geilizu.dom.result.ResponseData;
import com.geilizu.dom.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2022-07-12 17:42:43
 */
@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/save")
    public ResponseData save(@RequestBody Student student){
        boolean save = this.studentService.save(student);
        if(!save){
            return new ResponseData(ResponseCode.DATAERROR);
        }
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @GetMapping("/list/{page}/{size}")
    public ResponseData list(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Map<String,Object> map = this.studentService.list(page, size);
        return new ResponseData("0","success", (Long) map.get("count"), map.get("studentList"));
    }

    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchDto searchDto){
        Map<String,Object> map=studentService.search(searchDto);
        return new ResponseData("200","success", (Long) map.get("count"),map.get("studentList"));
    }

    @DeleteMapping("/deleteById/{id}")
    public ResponseData deleteById(@PathVariable("id") Integer id) {
        Integer del = this.studentService.deleteById(id);
        if(del>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.DATAERROR);
    }


    @PutMapping("/update")
    public ResponseData update(@RequestBody Student student){
        Integer update = this.studentService.updateById(student);
        if(update>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FAILED);
        }

    }
    @GetMapping("/findById/{id}")
    public ResponseData findById(@PathVariable("id") Integer id) {
        Student student = studentService.findById(id);
        return new ResponseData(ResponseCode.SUCCESS, student);
    }

}

